#ifndef TRACKBALL_H
#define TRACKBALL_H

#include <CEGUI/CEGUISystem.h>
#include <Ogre.h>
#include <OgreEventListeners.h>
#include <OgreCEGUIRenderer.h>

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Interface of Trackball
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Trackball : public Ogre::MouseListener,public Ogre::MouseMotionListener
{
public:
  enum ClickAction
  {
    NO_CLICK_ACTION,
    ZOOM_ON_PIXEL,
    ZOOM_TO_FIT,
    RAP_FROM_PIXEL,
    RAP_IS_CENTER,
    CENTER_SCENE,
    SHOW_ENTIRE_SCENE,
    ALIGN_CAMERA
  };
  enum MouseAction
  {
    NO_MOUSE_ACTION,
    ROTATE,
    ZOOM,
    TRANSLATE,
    MOVE_FORWARD,
    LOOK_AROUND,
    MOVE_BACKWARD,
    SCREEN_ROTATE,
    ROLL,
    SCREEN_TRANSLATE,
    ZOOM_ON_REGION
  };

  Trackball(CEGUI::OgreCEGUIRenderer*,CEGUI::System*,Ogre::Camera*,Ogre::SceneManager*);
  
  const Ogre::Vector3& sceneCenter() const;
  void setSceneCenter(const Ogre::Vector3&);
  const Ogre::Real sceneRadius() const;
  void setSceneRadius(Ogre::Real r);

  
  virtual void mousePressed(Ogre::MouseEvent*);
  virtual void mouseReleased(Ogre::MouseEvent*);

  virtual void mouseClicked(Ogre::MouseEvent*);
  virtual void mouseEntered(Ogre::MouseEvent*);
  virtual void mouseExited(Ogre::MouseEvent*);
  
  virtual void mouseMoved(Ogre::MouseEvent*);
  virtual void mouseDragged(Ogre::MouseEvent*);
protected:
  Ogre::Real distanceRevolveAroundPointToCamera() const;
  void align();
  void showEntireScene();
private:
  CEGUI::OgreCEGUIRenderer* mGUIRenderer;
  CEGUI::System*            mGUISystem;  
  Ogre::Camera*             mCamera;
  Ogre::SceneManager*       mSceneManager;
  Ogre::Vector3             mRevolveAroundPoint;
  Ogre::Vector3             mSceneCenter;
  float                     mSceneRadius;
  MouseAction               mMouseAction;
  Ogre::Timer*              mButtonTimers[3];
  bool                      mButtonDown[3];
  Ogre::RaySceneQuery*      mRaySceneQuery;
};
#endif // TRACKBALL_H
